
function swakSmackPotCalc() {
//	alert('swakSmackPotCalc entry');

	//check to see if open/showing..if so, close
	var ssp = swaksmack.SmackShopDoc.getElementById('swakSmackPot');
	if (ssp != null) {
		try {
			if (ssp.style.width!='0px') {
				ssp.style.width='0px';
				ssp.style.height='0px';
				return;
			}
		}
		catch (x){}
	}

	var retail = swaksmack.SmackShopDoc.getElementById('retailPrice');
	if (retail == null) return;
	var spt = swaksmack.SmackShopDoc.getElementById('smackPotTable');
	if (spt == null) return;
	
//	alert('pre get smackpot');
	var spotDiv = swaksmack.SmackShopDoc.getElementById('smackPot');
//	alert('smackpop div ' + spotDiv);
	var cf = getElementsByClassName('clearFloat',spotDiv);
	
//	alert(spt.offsetParent.offsetLeft + ' table offsetLeft  ' + spt.offsetLeft );
//	alert(spt.offsetParent.offsetTop + ' table offsetParent offsetTop  ' + spt.offsetTop);
//	var ttop = -spt.offsetTop + 225;  //spt.offsetParent.offsetTop 
//	var lleft =  spt.offsetLeft +185; //spt.offsetParent.offsetLeft +
	
	var ttop = 0;
	var lleft = 0;
	
	//var ssp = swaksmack.SmackShopDoc.getElementById('swakSmackPot');
	
	var price = retail.textContent;
	//alert('price ' + price);
	if (ssp == null) {
		var str = swakBuildSmackPotHelp(price, ttop, lleft);
		//alert('swakSmackPotCalc done building str');
		//var spheaderDiv = swaksmack.SmackShopDoc.getElementById('smackPot'); //smackPotHeader
		
		
		 var spresultDiv = swaksmack.SmackShopDoc.getElementById('smackPotResults'); //smackPotHeader
		
			var spdiv = swaksmack.SmackShopDoc.createElement('div');
			spdiv.setAttribute('id','swakSmackPotContainer');
			spdiv.innerHTML = str;
			//alert(spotDiv + ' spotDiv, pre insert cf length ' + cf.length);
			//spheaderDiv.appendChild(spdiv);
			if (cf.length == 1) {
				spotDiv.insertBefore(spdiv,cf[0]);
			}
			else if (cf.length == 16) {
				spotDiv.insertBefore(spdiv,cf[15]); //was cf[0]
				//	spotDiv.insertBefore(spdiv,spresultDiv); //was cf[0]
			}
			else {
				alert('Something has changed...this makes Mindstorm cry.');
			}

}
else {
	//alert('show, dont build');
	var spdiv = swaksmack.SmackShopDoc.getElementById('swakSmackPotContainer'); 
	var str =  swakBuildSmackPotHelp(price,ttop,lleft);
	spdiv.innerHTML = str; 
	ssp.style.width='150px';
	ssp.style.height='450px';
}

//var retail = swaksmack.SmackShopDoc.getElementById('retailPrice');
//alert('calling smackpot money ranges');
//swakBuildSmackPotPriceRanges(retail.textContent);

}

function swakBuildSmackPotHelp(retail, top, left) {
	
	var str='';
		str+='<div style="z-index: 9999; width: 150px; height: 450px;overflow: hidden; position: relative; visibility: visible; top: ' + top + 'px; left: ' + left + 'px; background: #660000;" id="swakSmackPot">';
		str+='            <script language="javascript">';
		str+='            function smakspClose()';
		str+='            {';
		str+='               document.getElementById(\'swakSmackPot\').style.width=\'0px\';';
		str+='               document.getElementById(\'swakSmackPot\').style.height=\'0px\'';		
		str+='            }';
		str+='            <\/script>';
		str+=swakBuildSmackPotPriceRanges(retail);
		str+='<\/div>';
		return str;
	
}


function swakBuildSmackPotPriceRanges(retailPrice) {
	//alert('swakBuildSmackPotPriceRanges entry ' + retailPrice);
	if (retailPrice == null) return null;
	retailPrice = retailPrice.replace(/\$/g, "");
	var price = parseFloat(retailPrice);
	if (isNaN(price) == true) return null;
	//alert('retail is a price ' + price);
	var d0 = price;
	var d5 = swakMoneyFormat(price*0.95); //0-5
	var d10 = swakMoneyFormat(price*0.9); //5-10
	var d15 = swakMoneyFormat(price*0.85); //10-15
	var d20 = swakMoneyFormat(price*0.8); //15-20
	var d25 = swakMoneyFormat(price*0.75);
	var d30 = swakMoneyFormat(price*0.7);
	var d35 = swakMoneyFormat(price*0.65);
	var d40 = swakMoneyFormat(price*0.6);
	var d45 = swakMoneyFormat(price*0.55);
	var d50 = swakMoneyFormat(price*0.5);
	var d55 = swakMoneyFormat(price*0.45);
	var d60 = swakMoneyFormat(price*0.4);
	var d65 = swakMoneyFormat(price*0.35);
	var d70 = swakMoneyFormat(price*0.3); //65-70
	var d100 = swakMoneyFormat(0.00); //70-100
	
	//alert(d0 + ', d35: ' +d35 + ', ' + d100);
	var r05 = swakBuildRange(d0,d5);
	var r10 = swakBuildRange(d5,d10);
	var r15 = swakBuildRange(d10,d15);
	var r20 = swakBuildRange(d15,d20);
	var r25 = swakBuildRange(d20,d25);
	var r30 = swakBuildRange(d25,d30);
	var r35 = swakBuildRange(d30,d35);
	var r40 = swakBuildRange(d35,d40);
	var r45 = swakBuildRange(d40,d45);
	var r50 = swakBuildRange(d45,d50);
	var r55 = swakBuildRange(d50,d55);
	var r60 = swakBuildRange(d55,d60);
	var r65 = swakBuildRange(d60,d65);
	var r70 = swakBuildRange(d65,d70);
	var r100 = swakBuildRange(d70,d100);
//	alert('range 05 '+ r05);
//	alert('range 100 '+ r100);
	
	var table = '<table cellspacing="0" cellpadding="0" border="0">';
	table+='<tr class=\'swakspheader\'><td>Range</td><td>Price</td></tr>';
	table+=buildTableRow('0-5',r05,'swakodd');
	table+=buildTableRow('5-10',r10,'swakeven');
	table+=buildTableRow('10-15',r15,'swakodd');
	table+=buildTableRow('15-20',r20,'swakeven');
	table+=buildTableRow('20-25',r25,'swakodd');
	table+=buildTableRow('25-30',r30,'swakeven');
	table+=buildTableRow('30-35',r35,'swakodd');
	table+=buildTableRow('35-40',r40,'swakeven');
	table+=buildTableRow('40-45',r45,'swakodd');
	table+=buildTableRow('45-50',r50,'swakeven');
	table+=buildTableRow('50-55',r55,'swakodd');
	table+=buildTableRow('55-60',r60,'swakeven');
	table+=buildTableRow('60-65',r65,'swakodd');
	table+=buildTableRow('65-70',r70,'swakeven');
	table+=buildTableRow('70-100',r100,'swakodd');
	table+='            <tr>';
	table+='            <td colspan="2">';
	table+='            <input type="button" value="Close" onclick="smakspClose();" style="width: 119px"\/><\/td>';
	table+='            <\/tr>';
	table+='</table>';
	
//	alert('table ' + table);
	return table;
}

function buildTableRow(per,range,class) {
	var str = '';
	if (class != null) {
		str+='<tr class=\'' + class + '\'><td>'+per;
	}
	else {
		str+='<tr><td>'+per;
	}
	str+='</td><td>' +range;
	str+='</td></tr>';
	return str;
}

function swakBuildRange(num1, num2) {
	if (num1 == null || num2 == null) return '???';
	if (num2 == 0) num2 = '0.00';
	var range = '$' +num1 +' - $' +num2;
	return range;
}

function swakMoneyFormat(num) {
	return Math.round(num*100)/100;
}